package com.lb.shopguide.ui.view.chart;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.lb.shopguide.entity.local.LineBean;
import com.lb.shopguide.util.lb.BizUtil;
import com.lb.shopguide.util.lb.PathAnimHelper;

import java.util.ArrayList;

/**
 * Created by kang on 2018/2/27.
 */

public class LbLineView extends View {
    private int mViewWidth; //view的宽
    private int mViewHeight;    //view的高

    private Paint mBaseLinePaint;
    private Paint mDatePaint;
    private Paint mPathPaint;
    private Path mPath;
    private Path mDisPath;

    private PathMeasure mPathMeasure;

    //    private PathMeasure mTargetTempPathMeasure;
//    private Path mTargetTempPath;
    /**
     * 柱形绘制进度
     */
//    private float mRectFration;

    private ValueAnimator mValueAnimator;

    private int chartHeight = 332;
    private int lineHeight = 272;

    private int amount = 4;
    private int amountWidth;
    private float maxData;

    protected ValueAnimator mAnimator;//动画对象

    private ArrayList<LineBean> mList;

    private float density;
    private float scaleDensity;

    private PathAnimHelper helper;

    public LbLineView(Context context) {
        super(context);
    }

    public LbLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initParams();

        initPaint();

        initPath();
    }

    public LbLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initParams() {
        density = getResources().getDisplayMetrics().density / 2;
        scaleDensity = getResources().getDisplayMetrics().density / 2;

        chartHeight = (int) (chartHeight * density);
        lineHeight = (int) (lineHeight * density);

        initAnimHelper();
    }

    private void initPaint() {
        mBaseLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBaseLinePaint.setAntiAlias(true);

        mDatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDatePaint.setAntiAlias(true);

        mPathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPathPaint.setAntiAlias(true);
        mPathPaint.setStrokeJoin(Paint.Join.ROUND);
        mPathPaint.setStyle(Paint.Style.STROKE);
        mPathPaint.setStrokeWidth(2 * scaleDensity);
//        mPath.lineTo(end.x, end.y);
        mPathPaint.setColor(Color.rgb(247, 85, 78));
    }

    private void initPath() {
        mPath = new Path();
        mDisPath = new Path();
    }

    private void initAnimHelper() {
        helper = new PathAnimHelper(this, mPath, mDisPath);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mViewWidth = getMeasuredWidth();
        mViewHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (checkData()) {
            return;
        }

        drawLines(canvas);
        canvas.drawPath(mDisPath, mPathPaint);
    }

    private boolean checkData() {
        boolean flag = false;
        if (mList == null) {
            flag = true;
        } else if (mList.size() == 0) {
            flag = true;
        }
        return flag;
    }


    public void setData(ArrayList<LineBean> list, float maxData) {
        if (mPath != null) {
            mPath.reset();
        }
        prepareData(list, maxData);
        startAnim();
    }

    private void startAnim() {
        initAnimHelper();
        helper.setInfinite(false);
        helper.setAnimTime(1500);
        helper.startAnim();
    }

    private void prepareData(ArrayList<LineBean> list, float maxData) {
        mList = list;
        amount = mList.size();
        this.maxData = maxData;
        if (this.maxData == 0.0f) {
            this.maxData = 1;
        }
        if (amount != 0) {

        } else {
            return;
        }
        initPathData();
    }


    private void initPathData() {
        amountWidth = mViewWidth / amount;
        for (int i = 0; i < amount; i++) {
            LineBean bean = mList.get(i);
            if (i < amount - 1) {
                float inPro = bean.getValue() / maxData;
                float yAxis = (1 - inPro) * lineHeight + 40 * density;
                PointF start = new PointF(mViewWidth / amount / 2 + amountWidth * i, yAxis);

                LineBean endBean = mList.get(i + 1);
                float inProEnd = endBean.getValue() / maxData;
                float yAxisEnd = (1 - inProEnd) * lineHeight + 40 * density;
                PointF end = new PointF(mViewWidth / amount / 2 + amountWidth * (i + 1), yAxisEnd);
//                drawRoundLine(canvas, start, endPt);

                mPath.moveTo(start.x, start.y);
                if (end.y >= start.y) {
                    mPath.cubicTo((end.x - start.x) / 2 + start.x, start.y, (end.x - start.x) / 2 + start.x, end.y, end.x, end.y);
                } else {
                    mPath.cubicTo((end.x - start.x) / 2 + start.x, start.y, (end.x - start.x) / 2 + start.x, end.y, end.x, end.y);
                }
            }
        }
        mPathMeasure = new PathMeasure(mPath, false);
    }

    private void drawLines(Canvas canvas) {
        amountWidth = mViewWidth / amount;
        drawBaseLine(canvas);
        for (int i = 0; i < amount; i++) {
            LineBean bean = mList.get(i);
            drawLines(canvas, bean, i);
        }
    }

    private void drawCircle(Canvas canvas, LineBean bean, int pos) {
        float inPro = bean.getValue() / maxData;
        float yAxis = (1 - inPro) * lineHeight + 40 * density;

        mBaseLinePaint.setColor(Color.rgb(247, 85, 78));
        canvas.drawCircle(mViewWidth / amount / 2 + amountWidth * pos, yAxis, 6 * density, mBaseLinePaint);

//        mBaseLinePaint.setColor(Color.WHITE);
//        mBaseLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        canvas.drawCircle(mViewWidth / amount / 2 + amountWidth * pos, yAxis, 4 * density, mBaseLinePaint);

        float width = mDatePaint.measureText("" + bean.getValue());
        mDatePaint.setColor(Color.rgb(247, 85, 78));
        mDatePaint.setTextSize(20 * scaleDensity);
        drawProText(canvas, mViewWidth / amount / 2 + amountWidth * pos - width / 2, yAxis, "" + bean.getValue(), mDatePaint);

    }

    private void drawBaseLine(Canvas canvas) {
        mBaseLinePaint.setColor(Color.rgb(222, 222, 222));
        canvas.drawLine(0, chartHeight, mViewWidth, chartHeight, mBaseLinePaint);
    }

    private void drawLines(Canvas canvas, LineBean bean, int pos) {
        String text = bean.getDate();
        float textWidth = mDatePaint.measureText(text);
        drawDate(canvas, mViewWidth / amount / 2 - textWidth / 2 + amountWidth * pos, chartHeight, text);

        drawCircle(canvas, bean, pos);
    }


    private void drawDate(Canvas canvas, float baseX, float baseY, String text) {
        //画文字
        mDatePaint.setColor(Color.rgb(46, 46, 46));
        mDatePaint.setTextSize(20 * scaleDensity);
        Paint.FontMetrics fontMetrics = mDatePaint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight - fontMetrics.bottom;
        float newY = baseY + offY * 2;

        canvas.drawText(text, baseX, newY, mDatePaint);
    }


    private void drawProText(Canvas canvas, float baseX, float baseY, String text, Paint paint) {
        //画文字
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight - fontMetrics.bottom;
        float newY = baseY - offY;
        text = BizUtil.convertBigNumber(text, 2);
        canvas.drawText(text, baseX, newY, paint);
    }


}
